﻿using Kingdee.BOS.Core.BusinessFlow.ServiceArgs;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.List.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.ConvertElement;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DevSamPle.FormPlugin
{
    [Description("【表单插件】OnChangeConvertRuleEnumList 下推或选单时隐藏某些转换规则"), HotUpdate]
    public class CustOnChangeConvertRuleEnumList : AbstractDynamicFormPlugIn
    {
        public override void OnChangeConvertRuleEnumList(ChangeConvertRuleEnumListEventArgs e)
        {
            base.OnChangeConvertRuleEnumList(e);
            if (IsPush(e.Convertrules))
            {
                // 查询单据转换规则SQL：SELECT a.FID AS 转换规则内码,b.FNAME AS 转换规则名称,* FROM T_META_CONVERTRULE a LEFT JOIN T_META_CONVERTRULE_L b ON a.FID=b.FID AND b.FLOCALEID=2052 WHERE a.FSOURCEFORMID='PUR_PurchaseOrder' AND a.FTARGETFORMID='STK_InStock'
                var ruleId = "07b71530-81b6-415f-8edb-b30de2c46494";
                // 采购订单下推采购入库单时，移除某个单据转换规则
                e.ConvertRuleEnumList.RemoveAll(o => o.EnumId == ruleId);
            }
        }

        private bool IsPush(List<ConvertRuleElement> convertrules)
        {
            var formId = this.View.BillBusinessInfo.GetForm().Id;
            return convertrules != null && convertrules.Any(o => o.SourceFormId.EqualsIgnoreCase(formId));
        }
        private bool IsDraw(List<ConvertRuleElement> convertrules)
        {
            var formId = this.View.BillBusinessInfo.GetForm().Id;
            return convertrules != null && convertrules.Any(o => o.TargetFormId.EqualsIgnoreCase(formId));
        }
    }
}
